<?php
/**
 * Created by PhpStorm.
 * User: Dinh Thi
 * Date: 2/9/15
 * Time: 9:50 PM
 */

namespace MyApp\Application\Controller;


use MyApp\Application\Model\MatHangModel;
use DinhThi\MVC\Request;

class MatHangController extends MyAppAbstractController{
    public function __construct(){
        parent::__construct();
        parent::mustBeLogin();
    }

    public function manageAction(){

        $this->view->addHeaderScript('Public/js/MatHang/manage.js');

        $this->view->addHeaderScript('Public/plugin/JsTree/dist/jstree.min.js');
        $this->view->addHeaderStyle('Public/plugin/JsTree/dist/themes/default/style.min.css');

        $this->view->setTitle('Quản lý mặt hàng');
        $this->view->render('MatHang/manage');
    }
    public function nhaphangAction(){

        $this->view->addHeaderScript('Public/js/MatHang/NhapHang.js');

        $this->view->addHeaderScript('Public/plugin/JsTree/dist/jstree.min.js');
        $this->view->addHeaderStyle('Public/plugin/JsTree/dist/themes/default/style.min.css');

        $this->view->setTitle('Nhập hàng vào kho');
        $this->view->render('MatHang/nhap-hang');
    }

    public function chinhgiaAction(){
        $this->view->addHeaderScript('Public/plugin/autoNumberic/autoNumberic.js');

        $this->view->addHeaderScript('Public/js/MatHang/ChinhGia.js');

        $this->view->addHeaderScript('Public/plugin/JsTree/dist/jstree.min.js');
        $this->view->addHeaderStyle('Public/plugin/JsTree/dist/themes/default/style.min.css');

        $this->view->setTitle('Chỉnh giá bán');
        $this->view->render('MatHang/chinh-gia');
    }


    //=============================
    public function luuNhapKhoAction(){
        if (isset ($_POST['MatHang'])){
            $model = new MatHangModel();
            $ghichu = $_POST['GhiChu'];
            $mathang = $_POST['MatHang'];
            $soluong = $_POST['SoLuong'];

            $data = $model->NhapKho($ghichu, $mathang, $soluong);
            echo json_encode($data);
        }
    }

    public function ChiTietNhapKhoAction(){
        $request = Request::getInstance();
        if (isset($request->getParams()[2])){
            $model = new MatHangModel();
            echo json_encode($model->XemChiTietNhapKho($request->getParams()[2]));
        }
    }

    public function DanhSachNhapKhoAction(){
        $model = new MatHangModel();
        echo json_encode($model->DanhSachNhapKho());
    }

    public function LuuChinhGiaAction(){
        if (isset ($_POST['MaMatHang'])){
            $model = new MatHangModel();
            $mamathang  = $_POST['MaMatHang'];
            $giagoc     = $_POST['GiaGoc'];
            $giaban     = $_POST['GiaBan'];

            $data = $model->ThemDieuChinhGia($mamathang, $giagoc, $giaban);
            echo json_encode($data);
        }
    }

    public function ThongTinChinhGiaAction(){
        $request = Request::getInstance();
        $id = $request->getParams()[2];

        $model = new MatHangModel();
        echo json_encode($model->ThongTinDieuChinhGia($id));
    }

    public function ThongTinMatHangAction(){
        $request = Request::getInstance();
        $id = $request->getParams()[2];

        $model = new MatHangModel();
        echo json_encode($model->ThongTinMatHang($id));
    }
    public function ThongTinMatHangTheoMaAction(){
        $request = Request::getInstance();
        $id = $request->getParams()[2];

        $model = new MatHangModel();
        echo json_encode($model->ThongTinMatHangTheoMa($id));
    }

    public function DanhSachMatHangAction(){
        $model = new MatHangModel();
        echo json_encode($model->TatCaMatHang());
    }

    public function HangTonKhoAction(){
        if (isset ($_POST['min'])){
            $min = $_POST['min'];
            $max = $_POST['max'];
            $model = new MatHangModel();
            echo json_encode($model->KiemTraHangTonKho($min, $max));
        }
    }
} 